package Pursuit_Client;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author Duyun Chen
 *
 */
public class PursuitDebugger extends JFrame {
	
	private static final long serialVersionUID = 3302923964576391682L;
	private LinkedHashMap<JLabel, JTextField> valueFields = new LinkedHashMap<JLabel, JTextField>();
	private final int FIELD_SIZE = 30;
	
	public PursuitDebugger() {
		super("Pursuit Client Debugger");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //don't remove from memory. Just hide it.
		setResizable(false); //resize causes crazy glitches.
		
		update();
		setVisible(false); //initially don't show. Wait till "Launch" fires event.
	}
	
	//this adds a field to the debugger GUI
	public boolean addField(String name) {
		if (!valueFields.containsKey(name)) {
			JLabel label = new JLabel(name);
			JTextField field = new JTextField(FIELD_SIZE);
			label.setLabelFor(field);
			valueFields.put(label, field);
			update();
			return true; //successfully added value field
		}
		return false; //not successfully added value field. Caused by duplicate field name entries.
	}
	
	public boolean removeField(String name) {
		if (!valueFields.containsKey(name)) {
			valueFields.remove(name);
			update();
			return true; //successfully removed value field
		}
		return false; //field is not in HashMap
	}
	
	//this updates the value in a field identified by the fieldName (JLabel)
	public void updateField(String fieldName, String value) {
		Set<JLabel> keys = valueFields.keySet();
		Iterator<JLabel> ii = keys.iterator();
		JTextField field = null;
		//This is SUCH an inefficient way of getting the JTextArea that
		//corresponds to the fieldName, but seems like it's the only way. 
		while (ii.hasNext()) {
			JLabel label = ii.next();
			if (label.getText().equals(fieldName)) {
				field = valueFields.get(label);
				break;
			}
		}
		
		if (field != null) {
			field.setText(value);
		}
		else {
			return;
		}
	}
	
	//re-layout the GUI to account for new/deleted value fields
	private void update() {
		setContentPane(createFieldsPanel());
		pack();
	}
	
	//create the content JPanel
	private JPanel createFieldsPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.BOTH;
		
		//add the JLabels and JTextFields in valueFields into the panel
		Set<JLabel> keys = valueFields.keySet();
		if (valueFields.isEmpty()) return panel;
		
		Iterator<JLabel> ii = keys.iterator();
		while (ii.hasNext()) {
			c.gridx = 0;
			JLabel label = ii.next();
			panel.add(label, c);
			c.gridx++;
			panel.add(valueFields.get(label), c);
			c.gridy++;
		}
		
		return panel;
	}
}
